package com.meituan.banma.common.net.request;

import android.os.Build;
import android.text.TextUtils;

import com.meituan.banma.base.net.antiReptile.AntiReptileParams;
import com.meituan.banma.bizcommon.util.TteModel;
import com.meituan.banma.common.net.listener.IResponseListener;
import com.meituan.banma.common.util.Constants;
import com.meituan.banma.locate.bean.LocationInfo;
import com.meituan.banma.location.LocationDataBridge;
import com.meituan.banma.main.bean.AppInfo;
import com.meituan.banma.main.model.AppPrefs;
import com.meituan.banma.modularity.CrowdSourceHttpConfig;
import com.meituan.banma.mutual.sceneevent.SceneBusinessManager;

/**
 * Created by zhengxiaoqiang on 15/3/31.
 */
public abstract class BaseRequest extends MyRequest {

    public BaseRequest(String relativeUrl, IResponseListener listener) {
        super(relativeUrl, listener);
    }

    @Override
    protected void addCommonParams() {

        //Add common params for new api
        addParam("osType", 1); //0:PC, 1:android, 2:iOS
        addParam("appVersion", AppInfo.appVersion);
        addParam("appVersionCode", AppInfo.appCode);
        addParam("uuid", AppInfo.getUUIDNotNull());
        addParam("appType", Constants.type);
        addParam("mtUserToken", AppPrefs.getToken());
        addParam("mtUserId", AppPrefs.getUserId());

        addParam("city_id", AppPrefs.getRiderCityId());
        addParam("workCityId",AppPrefs.getRiderWorkingCityId());

        addParam("osVersion", String.valueOf(Build.VERSION.SDK_INT));
        // 公参中的运力管理线仅用于预案降级的维度使用，业务需求要使用该参数需要单独评估，不要直接使用
        addParam("manageLineCode", String.valueOf(AppPrefs.getManageLine()));

        if (!TextUtils.isEmpty(RcpUtil.rcp)) {
            addParam("cp",RcpUtil.rcp);
        }

        if (CrowdSourceHttpConfig.canSetLbs("/" + getRelativeUrl())) {
            addLocationParams();
        }

        String scene = SceneBusinessManager.genSceneParams("/" + getRelativeUrl());
        if (!TextUtils.isEmpty(scene)) {
            addParam("scene", scene);
        }

        if (!TextUtils.isEmpty(TteModel.getEdk())) {
            addParam("edk", TteModel.edk);
        }


        //反爬验证相关参数
        addParam(AntiReptileParams.KEY_RISK_LEVEL, AntiReptileParams.RISK_LEVEL_SLIDE);
        addParam(AntiReptileParams.KEY_CODE, AntiReptileParams.CODE_DATA_REQUEST);
    }

    protected void addLocationParams(){
        LocationInfo info = LocationDataBridge.getLastLocationInfo();

        if(info != null) {
            addParam("latitude", info.getLatitude());
            addParam("longitude", info.getLongitude());
            addParam("provider", info.getProvider());
            addParam("accuracy", info.getAccuracy());
            addParam("speed", info.getSpeed());
        }
    }



    @Override
    protected boolean enableSignature() {
        return true;
    }
}
